
import { defineStore } from 'pinia'
import { ref } from 'vue'
import axios from 'axios'
// 导入定义的类型声明文件
import type {channelslist_type, channels_type}  from '@/types/MyData'
import type {chan_News, chan_News_List}  from '@/types/MyData'

export const userChannel = defineStore('counter', () => {
  const flag = ref<number>(0) // 设置当前选项的高亮
  const ChannelList = ref<channelslist_type[]>([]) // 用于存储 tab 栏标题的数据

  // 获取新闻的tab栏列表
  const GetChannelList = async() => {
    const res= await axios.get<channels_type>('http://geek.itheima.net/v1_0/channels')
    ChannelList.value= res.data.data.channels // 获取的数据返回给仓库
  }

  // 获取频道的新闻列表
  const channel_new_list = ref<chan_News_List[]>([])
  // GetChannel_New_list  --携带两个参数-- ： -频道的ID- ， -当前的时间戳-
  const GetChannel_New_list = async(channel_id: number, timestamp: number) => {
    const res = await axios.get<chan_News>('http://geek.itheima.net/v1_0/articles',{
      params:{
        channel_id,
        timestamp
      }
    }
    )
    console.log(res.data)
    
    channel_new_list.value = res.data.data.results // 获取的数据返回给仓库
  }
  // 暴露方法
  return { GetChannelList, ChannelList, flag, GetChannel_New_list, channel_new_list }
})
